package com.inhim.pjhim.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderBean;
import com.inhim.pjhim.utils.GlideCircleUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import fm.jiecao.jcvideoplayer_lib.JCUtils;


public class SearchReultListAdapter extends RecyclerView.Adapter<SearchReultListAdapter.ViewHolder> {
    private List<ReaderBean.PageListBean> listBeans = new ArrayList<>();
    private Context context;

    public SearchReultListAdapter(Context context) {
        this.context = context;
    }

    public void setListBeans(List<ReaderBean.PageListBean> listBeans) {
        this.listBeans = listBeans;
        notifyDataSetChanged();
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_search_content, viewGroup, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int i) {
        ReaderBean.PageListBean data = listBeans.get(i);
        holder.tvContent.setText(data.getTitle());
        Glide.with(context).load(data.getCover())
                .transform(new GlideCircleUtils.GlideRoundTransform(context, 6))
                .into(holder.ivCover);
        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemClick(data);
                }
            }
        });
        //1文章 2视频 3音频
        if("1".equals(data.getType())){
            holder.tvTime.setVisibility(View.GONE);
        }else{
            if(!TextUtils.isEmpty(data.getDuration())){
                holder.tvTime.setVisibility(View.VISIBLE);
                holder.tvTime.setText("时长 "+ JCUtils.stringForTime2(Integer.valueOf(data.getDuration())));
            }else{
                holder.tvTime.setText("时长 00:00");
            }
        }
        if(TextUtils.isEmpty(data.getTagName())){
            holder.tvTag.setVisibility(View.GONE);
        }else {
            holder.tvTag.setVisibility(View.VISIBLE);
            holder.tvTag.setText(data.getTagName());
        }
    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvContent;
        public TextView tvTime;
        public TextView tvTag;
        public ImageView ivCover;
        public View root;

        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            root = itemView;
            tvContent = itemView.findViewById(R.id.tvContent);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvTag = itemView.findViewById(R.id.tvTag);
            ivCover = itemView.findViewById(R.id.ivCover);
        }
    }

    private ItemClickListener listener;

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    public interface ItemClickListener {
        void onItemClick(ReaderBean.PageListBean data);
    }
}
